using System;
using System.IO;
using BitMango.Diagnostics;
using JsonFx.Json;
using ProtoBuf.Meta;
using UnityEngine;

public class DataContainer<T> where T : class, new()
{
	public static T Data
	{
		get
		{
			if (DataContainer<T>.instance == null)
			{
				DataContainer<T>.instance = DataContainer<T>.LoadBinary();
			}
			return DataContainer<T>.instance;
		}
	}

	public static void SetPostfix(string pf)
	{
		UnityEngine.Debug.Log("DataContainer.SetPostfix : " + pf);
		DataContainer<T>.postfix = pf;
		ResourceFileSystem.subPath = pf;
		DWATTHelper.Prefix = "." + pf;
		FirebaseAnalyticsHelper.Prefix = "." + pf;
	}

	public static string GetPostfix()
	{
		return DataContainer<T>.postfix;
	}

	public static void Reload()
	{
		DataContainer<T>.instance = DataContainer<T>.LoadBinary();
	}

	public static T Load()
	{
		if (!JsonFileSystem.HasVaileFileSystem())
		{
			JsonFileSystem.SetFileSystem(new ResourceFileSystem());
		}
		ContextPoolAdapter.Reset();
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		string text = null;
		try
		{
			text = JsonFileSystem.Read(typeof(T).FullName);
		}
		catch (FileNotFoundException ex)
		{
			UnityEngine.Debug.LogWarning(ex.Message + " " + typeof(T).FullName + ".Load() Fail, Making empty one");
			return Activator.CreateInstance<T>();
		}
		T result = (T)((object)null);
		try
		{
			result = JsonReader.Deserialize<T>(text);
		}
		catch (JsonDeserializationException ex2)
		{
			int num;
			int num2;
			ex2.GetLineAndColumn(text, out num, out num2);
			UnityEngine.Debug.LogError(string.Concat(new object[]
			{
				"JsonDeserializationException : ",
				ex2.Message,
				"  on ",
				typeof(T).FullName,
				".txt Line:",
				num,
				"  Col:",
				num2
			}));
		}
		UnityEngine.Debug.Log(typeof(T).FullName + " Loading done - " + (Time.realtimeSinceStartup - realtimeSinceStartup));
		return result;
	}

	public static T LoadBinary()
	{
		if (!JsonFileSystem.HasVaileFileSystem())
		{
			JsonFileSystem.SetFileSystem(new ResourceFileSystem());
		}
		ContextPoolAdapter.Reset();
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		T result = (T)((object)null);
		string path;
		if (string.IsNullOrEmpty(DataContainer<T>.postfix))
		{
			path = typeof(T).FullName + ".bin";
		}
		else
		{
			path = typeof(T).FullName + "." + DataContainer<T>.postfix + ".bin";
		}
		TextAsset textAsset = Resources.Load(path) as TextAsset;
		MemoryStream source = new MemoryStream(textAsset.bytes);
		try
		{
			DataBuilder dataBuilder = new DataBuilder();
			result = (dataBuilder.Deserialize(source, null, typeof(T)) as T);
		}
		catch (Exception message)
		{
			UnityEngine.Debug.LogError(message);
		}
		UnityEngine.Debug.Log(string.Concat(new object[]
		{
			typeof(T).FullName,
			".",
			DataContainer<T>.postfix,
			" Binary Loading done - ",
			Time.realtimeSinceStartup - realtimeSinceStartup
		}));
		return result;
	}

	public static void Save(T target)
	{
		Debugger.Assert(target != null);
		UnityEngine.Debug.Log(typeof(T).FullName + " Saving");
		if (!JsonFileSystem.HasVaileFileSystem())
		{
			JsonFileSystem.SetFileSystem(new ResourceFileSystem());
		}
		JsonFileSystem.Write(typeof(T).FullName, JsonWriter.Serialize(target));
	}

	public static void SaveBinary(T target)
	{
		Debugger.Assert(target != null);
		UnityEngine.Debug.Log(typeof(T).FullName + "." + DataContainer<T>.postfix + " Saving");
		Directory.CreateDirectory(Application.dataPath + "/Data/Resources");
		string text;
		if (string.IsNullOrEmpty(DataContainer<T>.postfix))
		{
			text = typeof(T).FullName + ".bin.bytes";
		}
		else
		{
			text = typeof(T).FullName + "." + DataContainer<T>.postfix + ".bin.bytes";
		}
		FileStream fileStream = new FileStream(Application.dataPath + "/Data/Resources/" + text, FileMode.Create, FileAccess.Write, FileShare.Write);
		RuntimeTypeModel runtimeTypeModel = TypeModel.Create();
		runtimeTypeModel.UseImplicitZeroDefaults = false;
		runtimeTypeModel.Serialize(fileStream, target);
		fileStream.Close();
		UnityEngine.Debug.Log(text + " Saving Done");
	}

	public static void LoadBinaryFromFile(string fn)
	{
		if (!JsonFileSystem.HasVaileFileSystem())
		{
			JsonFileSystem.SetFileSystem(new ResourceFileSystem());
		}
		UnityEngine.Debug.Log("start loading down loaded file : " + fn);
		ContextPoolAdapter.Reset();
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		T t = (T)((object)null);
		if (File.Exists(fn))
		{
			UnityEngine.Debug.Log("File Exist : " + fn);
		}
		else
		{
			UnityEngine.Debug.Log("File not Exist : " + fn);
		}
		MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(fn));
		try
		{
			DataBuilder dataBuilder = new DataBuilder();
			t = (dataBuilder.Deserialize(memoryStream, null, typeof(T)) as T);
		}
		catch (Exception message)
		{
			UnityEngine.Debug.LogError(message);
		}
		UnityEngine.Debug.Log("Download Binary Loading done - " + (Time.realtimeSinceStartup - realtimeSinceStartup));
		DataContainer<T>.instance = t;
		memoryStream.Close();
	}

	protected static T instance;

	protected static string postfix = "MASTER";
}
